package com.xinyues.gamemanager.common;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class InputStreamUtil {
    private static Logger logger = LoggerFactory.getLogger(InputStream.class);
    /**
     * 
     * <p>Description:读取inputSteam数据，一般也应用于读取http的request数据 </p>
     * @param inputStream
     * @return
     * @author wang guang shuai 
     * @date  2019年12月30日 上午10:55:28
     *
     */
    public static String readInputStream(InputStream inputStream) {

        BufferedReader in = null;
        StringBuilder result = new StringBuilder();
        try {
            in = new BufferedReader(new InputStreamReader(inputStream));
            String line;
            while ((line = in.readLine()) != null) {
                result.append(line);
            }
        } catch (Exception e) {
            logger.error("http读取数据错误", e);
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (IOException ex) {
                logger.error("http读取数据错误", ex);
            }
        }
        if (result.length() == 0) {
            return null;
        }
        return result.toString();
    }
}
